//
//  CheckoutRecordViewController.m
//  CS Inventory Manager
//
//  Created by Billy Zoellers on 3/21/13.
//  Copyright (c) 2013 Transylvania University. All rights reserved.
//

#import "CheckoutRecordViewController.h"
#import "CheckoutHistoryNavigationViewController.h"

@interface CheckoutRecordViewController ()

@end

@implementation CheckoutRecordViewController
@synthesize theRecord,delegate;

- (IBAction)doneButton:(id)sender {

    [self dismissViewControllerAnimated:YES completion:nil];
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (!theRecord) {
        // get it from the delegate - we're being called staight from a nav controller
        [self setDelegate:self.navigationController];
        [self setTheRecord:[delegate passThroughRecord]];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger numRows = 0;
    
    switch (section) {
        case 0:
            numRows = 3;
            break;
        case 1:
            numRows = [[theRecord patrons] count];
            break;
        case 2:
            numRows = 6;
            break;
    }
    
    return numRows;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *title = nil;
    
    // depends on section
    switch (section) {
            // asset info
        case 0:
            title = @"Item Information";
            break;
        case 1:
            title = @"Patron Information";
            break;
        case 2:
            title = @"Checkout Information";
            break;
    }
    
    return title;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier;
    
    if (indexPath.section == 1) {
        CellIdentifier = @"checkoutRecordCellSign";
    } else {
        CellIdentifier = @"checkoutRecordCell";
    }
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    // signature image
    UIImage *image;
    
    // might need to format some dates
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MM/dd/yyyy"];
    
    // get image from web if needed
    if (indexPath.section == 1) {
        NSString *url = [NSString stringWithFormat:@"http://www.cs.transy.edu/zoellers/inventory/uploads/%d-%d.png",[theRecord checkoutID],[[[theRecord patrons] objectAtIndex:indexPath.row] id]];
        image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
    }
        
    switch (indexPath.section) {
        case 0:
            // item info - depends on the row
            switch (indexPath.row) {
                case 0:
                    cell.textLabel.text = @"Type";
                    cell.detailTextLabel.text = [theRecord itemType];
                    break;
                case 1:
                    cell.textLabel.text = @"Model";
                    cell.detailTextLabel.text = [theRecord itemModel];
                    break;
                case 2:
                    cell.textLabel.text = @"Asset No.";
                    cell.detailTextLabel.text = [theRecord assetNo];
                    break;
            }
            break;
        case 1:
            cell.textLabel.text = [[[theRecord patrons] objectAtIndex:indexPath.row] name];
            if (image) {
                cell.detailTextLabel.text = @"";
                cell.imageView.image = image;
                //this line will do it!
                cell.imageView.transform = CGAffineTransformMakeScale(40, 50);
            } else {
                cell.detailTextLabel.text = @"Signature unavailable";
            }
            break;
        case 2:
            // checkout info - depends on row
            switch (indexPath.row) {
                case 0:
                    cell.textLabel.text = @"Out Date";
                    cell.detailTextLabel.text = [dateFormat stringFromDate:[theRecord outDate]];
                    break;
                case 1:
                    cell.textLabel.text = @"Due Date";
                    cell.detailTextLabel.text = [dateFormat stringFromDate:[theRecord dueDate]];
                    break;
                case 2:
                    cell.textLabel.text = @"Return Date";
                    cell.detailTextLabel.text = [dateFormat stringFromDate:[theRecord inDate]];
                    break;
                case 3:
                    cell.textLabel.text = @"Out Notes";
                    cell.detailTextLabel.text = [theRecord outNotes];
                    break;
                case 4:
                    cell.textLabel.text = @"In Notes";
                    cell.detailTextLabel.text = [theRecord inNotes];
                    break;
                case 5:
                    cell.textLabel.text = @"Professor";
                    cell.detailTextLabel.text = [theRecord profName];
                    break;
            }
            break;            
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        return 110;
    }
    
    return 44;
}

@end
